package com.study.zouchao.superproject.mypublishtask.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.ZouImgLoader;
import com.study.zouchao.superproject.comtasklist.fragment.model.entity.TaskListResult;
import com.study.zouchao.superproject.mypublishtask.model.entity.MyPublishTaskListResult;
import com.study.zouchao.superproject.util.LogLongUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */

public class ChooseUserAdapter extends RecyclerView.Adapter<ChooseUserAdapter.MyViewHolder> {
    private List<MyPublishTaskListResult.TaskItemBean.EnrolledListBean> mDatas;
    private Context mContext;
    private IRvItemClickListener mListener;
    private View mFooterView;
    private static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    private static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的
    public ChooseUserAdapter(Context context, List<MyPublishTaskListResult.TaskItemBean.EnrolledListBean> datas) {
        mContext = context;
//        mDatas = new ArrayList<>();
//        mDatas.addAll(datas);
        mDatas = datas;
        LogLongUtil.logD("datas", datas.toString());
    }
    public void setOnItemClickListener(IRvItemClickListener listener) {
        mListener = listener;
    }
    @Override
    public ChooseUserAdapter.MyViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_user, parent, false);
        return new ChooseUserAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChooseUserAdapter.MyViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder != null) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                MyPublishTaskListResult.TaskItemBean.EnrolledListBean item = mDatas.get(position);
                if (item != null) {
                    ZouImgLoader.loadImage(mContext, holder.ivHeadimg, item.getHeadimg(), R.drawable.head_img);
                    holder.tvNickname.setText(item.getNickName());
                }
            }
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if(mFooterView == null) return mDatas.size();
        else return mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return TYPE_NORMAL;
    }

    /**
     * Holder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        View mItemView;
        ImageView ivHeadimg;
        TextView tvNickname;

        public MyViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            ivHeadimg = (ImageView) itemView.findViewById(R.id.id_iv_headimg);
            tvNickname = (TextView) itemView.findViewById(R.id.id_tv_nickname);

            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        if (mDatas.size()-1 < getAdapterPosition()) return;
                        if (getAdapterPosition() < 0)   return;

                        MyPublishTaskListResult.TaskItemBean.EnrolledListBean item = mDatas.get(getAdapterPosition());
                        mListener.onItemClick(mItemView, item);
                    }
                }
            });
        }

    }
    public interface IRvItemClickListener {
        void onItemClick(View v, MyPublishTaskListResult.TaskItemBean.EnrolledListBean item);
    }
}
